package com.ss.android.action.b;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.ss.android.d.SSDBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.json.JSONArray;
import org.json.JSONObject;

public final class ImpressionHelper implements Callback, ImpressionRecorder.a {
    final class e$a implements b {
        e$a() {

        }

        public final void a(com.ss.android.action.b.a arg1) {
        }

        public final void a(FeedViewHolder arg1) {
        }

        public final void a(String arg1, String arg2, String arg3, long arg4, String arg6, int arg7) {
        }

        public final void b(com.ss.android.action.b.a arg1) {
        }

        public final void b(FeedViewHolder arg1) {
        }
    }

    public static final class e$b {
        public e$b() {

        }

        public static void a(JSONObject arg4, String arg5, Object arg6) {
            int v3 = 11;
            if(!android.support.design.a.isTextEmpty(arg5) && arg6 != null) {
                try {
                    if(("key_name".equals(arg5)) && ((arg6 instanceof String)) && (((String)arg6).startsWith("news_local_"))) {
                        if(((String)arg6).length() > v3) {
                            arg4.put("city", ((String)arg6).substring(11));
                        }

                        arg4.put(arg5, "news_local");
                        return;
                    }

                    arg4.put(arg5, arg6);
                }
                catch(Exception v1) {
                    v1.printStackTrace();
                }
            }
        }
    }

    public HashMap<String, ImpressionRecorder> a;
    public volatile List<com.ss.android.model.b> b;
    public AtomicLong sessionId;
    public final AtomicInteger d;
    public final Handler e;
    public e$b f;
    private static ImpressionHelper instance;
    private e$a h;
    private long i;
    private boolean j;

    private ImpressionHelper() {
        this.a = new HashMap();
        this.sessionId = new AtomicLong();
        this.j = false;
        this.d = new AtomicInteger();
        this.e = new WeakHandler(Looper.getMainLooper(), ((Callback)this));
    }

    public static ImpressionHelper getInstance() {
        ImpressionHelper v0_1;
        Class v1 = ImpressionHelper.class;
        synchronized (v1){
            if(instance == null) {
                instance = new ImpressionHelper();
            }

            v0_1 = instance;
        }
        return v0_1;
    }

    private void a(long arg6, List arg8, boolean arg9) {
        if(!this.a.isEmpty()) {
            Iterator<Map.Entry<String, ImpressionRecorder>> v1 = this.a.entrySet().iterator();
            while(v1.hasNext()) {
                ImpressionRecorder v0 = (ImpressionRecorder) v1.next().getValue();
                if(v0 == null) {
                    continue;
                }

                String v2 = ((ImpressionRecorder)v0).b;
                if(android.support.design.a.isTextEmpty(v2)) {
                    continue;
                }

                JSONArray v3 = ((ImpressionRecorder)v0).a(arg9);
                if((arg9) && (v2.startsWith("subject_"))) {
                    v1.remove();
                }

                if(v3.length() <= 0) {
                    continue;
                }

                com.ss.android.model.b v4 = new com.ss.android.model.b();
                v4.keyName = v2;
                v4.listType = ((ImpressionRecorder)v0).a;
                v4.sessionId = arg6;
                v4.extra = ((ImpressionRecorder)v0).c;
                v4.jsonArray = v3;
                arg8.add(v4);
            }
        }
    }

    public static boolean a(int arg4, String arg5) {
        boolean v0 = true;
        try {
            int v2 = com.ss.android.action.bb.a().d;
            if(v2 < 0) {
                return v0;
            }

            if(v2 == 0) {
                return false;
            }

            switch(arg4) {
                case 1: {
                    if(arg5 != null && (arg5.startsWith("subject_"))) {
                        if((v2 & 2) <= 0) {
                            v0 = false;
                        }
                        else {
                        }
                        return v0;
                    }
                    if((v2 & 1) > 0) {
                        return v0;
                    }
                    return false;
                }
                case 2: {
                    if((v2 & 4) > 0) {
                        return v0;
                    }
                    return false;
                }
                case 3: {
                    if(!"dongtai".equals(arg5)) {
                        if((v2 & 16) > 0) {
                            return v0;
                        }
                        return false;
                    }
                    if((v2 & 8) > 0) {
                        return v0;
                    }
                    return false;
                }
                case 4: {
                    if((v2 & 32) > 0) {
                        return v0;
                    }

                    return false;
                }
                case 5: {
                    if((v2 & 64) > 0) {
                    }
                    else {
                        v0 = false;
                    }

                    return v0;
                }
            }
            return v0;
        }
        catch(Exception v1) {
            return v0;
        }
    }

    public final b a(int arg3, String arg4, String arg5) {
        b v0;
        if(a(arg3, arg4)) {
            v0 = new ImpressionRecorder(arg3, arg4, arg5, null);
        }
        else {
            v0 = this.b();
        }

        return ((b)v0);
    }

    public final void a(b arg9, String arg10) {
        if(arg9 != null && !android.support.design.a.isTextEmpty(arg10)) {
            b v0 = null;
            if(!(arg9 instanceof ImpressionRecorder)) {
                arg9 = v0;
            }

            if(arg9 == null) {
                return;
            }

            int v1 = ((ImpressionRecorder)arg9).a;
            String v2 = ((ImpressionRecorder)arg9).c;
            com.ss.android.cc v0_1 = ComponentsManifest.findComponent(com.ss.android.cc.class);
            if(v0_1 == null) {
                return;
            }

            SSDBHelper v0_2 = (v0_1).getDBHelper();
            JSONArray v3 = ((ImpressionRecorder)arg9).a(true);
            if(v0_2 == null) {
                return;
            }

            if(v3.length() <= 0) {
                return;
            }

            com.ss.android.model.b v4 = new com.ss.android.model.b();
            v4.keyName = arg10;
            v4.listType = v1;
            v4.jsonArray = v3;
            v4.sessionId = this.sessionId.get();
            v4.extra = v2;
            ArrayList v5 = new ArrayList();
            ((List)v5).add(v4);
            v0_2.f(((List)v5));
            if(!Logger.debug()) {
                return;
            }

            Logger.d("ImpressionHelper", v1 + " " + arg10 + " " + v2 + " impression " + v3.toString());
        }
    }

    public final b b() {
        if(this.h == null) {
            this.h = new e$a();
        }

        return this.h;
    }

    public final void c() {
        int v1 = 101;
        if(!this.j) {
            if(!this.e.hasMessages(v1)) {
                this.e.sendEmptyMessageDelayed(v1, 25000);
            }

            this.j = true;
        }
    }

    public final void handleMsg(Message message) {
        int v9 = 101;
        long v2 = 0;
        switch(message.what) {
            case 100: {
               long  v0 = (message.obj instanceof Long) ? ((Long) message.obj).longValue() : v2;
                if(v0 > v2) {
                    List v2_1 = new ArrayList();
                    this.a(v0, ((List)v2_1), true);
                    AtomicInteger v1 = this.d;
                    synchronized (v1){
                        this.b = ((List)v2_1);
                        this.d.set(0);
                        this.d.notify();
                    }

                    this.e.removeMessages(v9);
                    this.i = System.currentTimeMillis();
                    this.j = false;
                }
                return;
            }
            case 101: {
                long v0 = this.sessionId.get();
                if(v0 <= v2) {
                    return;
                }

                v2 = System.currentTimeMillis();
                if(v2 - this.i < 25000) {
                    return;
                }

                this.e.removeMessages(v9);
                this.i = v2;
                ArrayList v2_1 = new ArrayList();
                this.a(v0, ((List)v2_1), false);
                if(!((List)v2_1).isEmpty()) {
                    try {
                        com.ss.android.cc v0_2 = ComponentsManifest.findComponent(com.ss.android.cc.class);
                        if(v0_2 == null) {
                        }else {
                            v0_2.getDBHelper().f((List) v2_1);
                        }
                    }
                    catch(Exception v0_1) {
                    }
                }

                this.j = false;
                return;
            }
        }

        return;


    }
}

